home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / DirectMusic / MusicTool / musictool.cpp < prev    next >
C/C++ Source or Header  |  2001-10-31  |  19KB  |  535 lines

  1. //-----------------------------------------------------------------------------
  2. // File: MusicTool.cpp
  3. //
  4. // Desc: Plays a primary segment using DirectMusic
  5. //
  6. // Copyright (c) 2000-2001 Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #define STRICT
  9. #include <windows.h>
  10. #include <basetsd.h>
  11. #include <commdlg.h>
  12. #include <commctrl.h>
  13. #include <dmusicc.h>
  14. #include <dmusici.h>
  15. #include <dxerr8.h>
  16. #include <cguid.h>
  17. #include <tchar.h>
  18. #include "resource.h"
  19. #include "DMUtil.h"
  20. #include "DXUtil.h"
  21. #include "EchoTool.h"
  22.  
  23.  
  24.  
  25.  
  26. //-----------------------------------------------------------------------------
  27. // Function-prototypes
  28. //-----------------------------------------------------------------------------
  29. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam );
  30. HRESULT OnInitDialog( HWND hDlg );
  31. HRESULT ProcessDirectMusicMessages( HWND hDlg );
  32. VOID    OnOpenSoundFile( HWND hDlg );
  33. HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName );
  34. HRESULT OnPlaySegment( HWND hDlg );
  35. VOID    EnablePlayUI( HWND hDlg, BOOL bEnable );
  36. HRESULT OnChangeTool( HWND hDlg );
  37.  
  38.  
  39.  
  40.  
  41. //-----------------------------------------------------------------------------
  42. // Defines, constants, and global variables
  43. //-----------------------------------------------------------------------------
  44. HINSTANCE               g_hInst                 = NULL;
  45. CMusicManager*          g_pMusicManager         = NULL;
  46. CMusicSegment*          g_pMusicSegment         = NULL;
  47. CEchoTool*              g_pEchoTool             = NULL;
  48. IDirectMusicTool*       g_pCurrentTool          = NULL;
  49. IDirectMusicGraph*      g_pGraph                = NULL;
  50. HANDLE                  g_hDMusicMessageEvent   = NULL;
  51.  
  52.  
  53.  
  54.  
  55. //-----------------------------------------------------------------------------
  56. // Name: WinMain()
  57. // Desc: Entry point for the application.  Since we use a simple dialog for 
  58. //       user interaction we don't need to pump messages.
  59. //-----------------------------------------------------------------------------
  60. INT APIENTRY WinMain( HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR pCmdLine, 
  61.                       INT nCmdShow )
  62. {
  63.     HWND    hDlg = NULL;
  64.     BOOL    bDone = FALSE;
  65.     int     nExitCode;
  66.     HRESULT hr; 
  67.     DWORD   dwResult;
  68.     MSG     msg;
  69.  
  70.     g_hInst = hInst;
  71.  
  72.     // Display the main dialog box.
  73.     hDlg = CreateDialog( hInst, MAKEINTRESOURCE(IDD_MAIN), 
  74.                          NULL, MainDlgProc );
  75.  
  76.     while( !bDone ) 
  77.     { 
  78.         dwResult = MsgWaitForMultipleObjects( 1, &g_hDMusicMessageEvent, 
  79.                                               FALSE, INFINITE, QS_ALLEVENTS );
  80.         switch( dwResult )
  81.         {
  82.             case WAIT_OBJECT_0 + 0:
  83.                 // g_hDPMessageEvent is signaled, so there are
  84.                 // DirectPlay messages available
  85.                 if( FAILED( hr = ProcessDirectMusicMessages( hDlg ) ) ) 
  86.                 {
  87.                     DXTRACE_ERR( TEXT("ProcessDirectMusicMessages"), hr );
  88.                     return FALSE;
  89.                 }
  90.                 break;
  91.  
  92.             case WAIT_OBJECT_0 + 1:
  93.                 // Windows messages are available
  94.                 while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) 
  95.                 { 
  96.                     if( !IsDialogMessage( hDlg, &msg ) )  
  97.                     {
  98.                         TranslateMessage( &msg ); 
  99.                         DispatchMessage( &msg ); 
  100.                     }
  101.  
  102.                     if( msg.message == WM_QUIT )
  103.                     {
  104.                         nExitCode = (int)msg.wParam;
  105.                         bDone     = TRUE;
  106.                         DestroyWindow( hDlg );
  107.                     }
  108.                 }
  109.                 break;
  110.         }
  111.     }
  112.  
  113.     return nExitCode;
  114. }
  115.  
  116.  
  117.  
  118.  
  119. //-----------------------------------------------------------------------------
  120. // Name: MainDlgProc()
  121. // Desc: Handles dialog messages
  122. //-----------------------------------------------------------------------------
  123. INT_PTR CALLBACK MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  124. {
  125.     HRESULT hr;
  126.  
  127.     switch( msg ) 
  128.     {
  129.         case WM_INITDIALOG:
  130.             if( FAILED( hr = OnInitDialog( hDlg ) ) )
  131.             {
  132.                 DXTRACE_ERR( TEXT("OnInitDialog"), hr );
  133.                 MessageBox( hDlg, "Error initializing DirectMusic.  Sample will now exit.", 
  134.                                   "DirectMusic Sample", MB_OK | MB_ICONERROR );
  135.                 PostQuitMessage( IDABORT );
  136.                 return FALSE;
  137.             }
  138.  
  139.             break;
  140.  
  141.         case WM_COMMAND:
  142.             switch( LOWORD(wParam) )
  143.             {
  144.                 case IDC_SOUNDFILE:
  145.                     OnOpenSoundFile( hDlg );
  146.                     break;
  147.  
  148.                 case IDCANCEL:
  149.                     PostQuitMessage( IDCANCEL );
  150.                     break;
  151.  
  152.                 case IDC_PLAY:
  153.                     if( FAILED( hr = OnPlaySegment( hDlg ) ) )
  154.                     {
  155.                         DXTRACE_ERR( TEXT("OnPlaySegment"), hr );
  156.                         MessageBox( hDlg, "Error playing DirectMusic segment. "
  157.                                     "Sample will now exit.", "DirectMusic Sample", 
  158.                                     MB_OK | MB_ICONERROR );
  159.                         PostQuitMessage( IDABORT );
  160.                     }
  161.                     break;
  162.  
  163.                 case IDC_STOP:
  164.                     g_pMusicSegment->Stop( DMUS_SEGF_BEAT ); 
  165.                     EnablePlayUI( hDlg, TRUE );
  166.                     break;
  167.  
  168.                 case IDC_TOOL_COMBO:
  169.                     OnChangeTool( hDlg );
  170.                     break;
  171.  
  172.                 default:
  173.                     return FALSE; // Didn't handle message
  174.             }
  175.             break;
  176.  
  177.         case WM_DESTROY:
  178.             // Cleanup everything
  179.             SAFE_DELETE( g_pMusicSegment );
  180.             SAFE_DELETE( g_pMusicManager );
  181.             SAFE_DELETE( g_pEchoTool );
  182.             CloseHandle( g_hDMusicMessageEvent );
  183.             break; 
  184.  
  185.         default:
  186.             return FALSE; // Didn't handle message
  187.     }
  188.  
  189.     return TRUE; // Handled message
  190. }
  191.  
  192.  
  193.  
  194.  
  195. //-----------------------------------------------------------------------------
  196. // Name: OnInitDialog()
  197. // Desc: Initializes the dialogs (sets up UI controls, etc.)
  198. //-----------------------------------------------------------------------------
  199. HRESULT OnInitDialog( HWND hDlg )
  200. {
  201.     HRESULT hr;
  202.     LONG lIndex;
  203.  
  204.     // Set the icon for this dialog.
  205.     HICON hIcon = LoadIcon( g_hInst, MAKEINTRESOURCE( IDR_MAINFRAME ) );
  206.     SendMessage( hDlg, WM_SETICON, ICON_BIG,   (LPARAM) hIcon );  // Set big icon
  207.     SendMessage( hDlg, WM_SETICON, ICON_SMALL, (LPARAM) hIcon );  // Set small icon
  208.  
  209.     g_hDMusicMessageEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  210.     g_pMusicManager = new CMusicManager();
  211.  
  212.     // Init DirectMusic with a default audio path
  213.     hr = g_pMusicManager->Initialize( hDlg );
  214.     IDirectMusicPerformance8* pPerformance    = g_pMusicManager->GetPerformance();
  215.     IDirectMusicAudioPath8* pDefaultAudioPath = g_pMusicManager->GetDefaultAudioPath();
  216.  
  217.     // Create a DirectMusicGraph, and tell the preformance about it
  218.     hr = pDefaultAudioPath->GetObjectInPath( 0, DMUS_PATH_PERFORMANCE_GRAPH, 0,
  219.                                              GUID_NULL, 0, IID_IDirectMusicGraph, 
  220.                                              (LPVOID*) &g_pGraph );
  221.     if( FAILED( hr ) )
  222.         return DXTRACE_ERR( TEXT("GetObjectInPath"), hr );
  223.  
  224.     // Register segment notification
  225.     GUID guid = GUID_NOTIFICATION_SEGMENT;
  226.     if( FAILED( hr = pPerformance->AddNotificationType( guid ) ) )
  227.         return DXTRACE_ERR( TEXT("AddNotificationType"), hr );
  228.  
  229.     if( FAILED( hr = pPerformance->SetNotificationHandle( g_hDMusicMessageEvent, 0 ) ) )
  230.         return DXTRACE_ERR( TEXT("SetNotificationHandle"), hr );
  231.  
  232.     g_pEchoTool = new CEchoTool();
  233.  
  234.     // Init the UI
  235.     HWND hToolCombo = GetDlgItem( hDlg, IDC_TOOL_COMBO );
  236.     lIndex = (LONG)SendMessage( hToolCombo, CB_ADDSTRING, 0, (LPARAM) TEXT("None") );
  237.     SendMessage( hToolCombo, CB_SETITEMDATA, lIndex, (LPARAM) NULL );
  238.     lIndex = (LONG)SendMessage( hToolCombo, CB_ADDSTRING, 0, (LPARAM) TEXT("Echo Tool") );
  239.     SendMessage( hToolCombo, CB_SETITEMDATA, lIndex, (LPARAM) (IDirectMusicTool*) g_pEchoTool );
  240.     SendMessage( hToolCombo, CB_SETCURSEL, 0, 0 );
  241.  
  242.     // Load a default music segment 
  243.     TCHAR strFileName[MAX_PATH];
  244.     strcpy( strFileName, DXUtil_GetDXSDKMediaPath() );
  245.     strcat( strFileName, "sample.sgt" );
  246.     if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
  247.     {
  248.         // Set the UI controls
  249.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("No file loaded.") );
  250.     }
  251.  
  252.     return S_OK;
  253. }
  254.  
  255.  
  256.  
  257.  
  258. //-----------------------------------------------------------------------------
  259. // Name: OnOpenSoundFile()
  260. // Desc: Called when the user requests to open a sound file
  261. //-----------------------------------------------------------------------------
  262. VOID OnOpenSoundFile( HWND hDlg ) 
  263. {
  264.     static TCHAR strFileName[MAX_PATH] = TEXT("");
  265.     static TCHAR strPath[MAX_PATH] = TEXT("");
  266.  
  267.     // Get the default media path (something like C:\MSSDK\SAMPLES\DMUSIC\MEDIA)
  268.     if( '\0' == strPath[0] )
  269.     {
  270.         const TCHAR* szDir = DXUtil_GetDXSDKMediaPath();
  271.         strcpy( strPath, szDir );
  272.     }
  273.  
  274.     // Setup the OPENFILENAME structure
  275.     OPENFILENAME ofn = { sizeof(OPENFILENAME), hDlg, NULL,
  276.                          TEXT("DirectMusic Content Files\0*.sgt;*.mid;*.rmi\0All Files\0*.*\0\0"), NULL,
  277.                          0, 1, strFileName, MAX_PATH, NULL, 0, strPath,
  278.                          TEXT("Open Content File"),
  279.                          OFN_FILEMUSTEXIST|OFN_HIDEREADONLY, 0, 0,
  280.                          TEXT(".sgt"), 0, NULL, NULL };
  281.  
  282.     if( g_pMusicSegment )
  283.         g_pMusicSegment->Stop( 0 );
  284.  
  285.     // Update the UI controls to show the sound as loading a file
  286.     EnableWindow(  GetDlgItem( hDlg, IDC_PLAY ), FALSE);
  287.     EnableWindow(  GetDlgItem( hDlg, IDC_STOP ), FALSE);
  288.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Loading file...") );
  289.  
  290.     // Display the OpenFileName dialog. Then, try to load the specified file
  291.     if( TRUE != GetOpenFileName( &ofn ) )
  292.     {
  293.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Load aborted.") );
  294.         return;
  295.     }
  296.  
  297.     if( S_FALSE == LoadSegmentFile( hDlg, strFileName ) )
  298.     {
  299.         // Not a critical failure, so just update the status
  300.         SetDlgItemText( hDlg, IDC_FILENAME, TEXT("Could not create segment from file.") );
  301.     }
  302.  
  303.     // Remember the path for next time
  304.     strcpy( strPath, strFileName );
  305.     char* strLastSlash = strrchr( strPath, '\\' );
  306.     strLastSlash[0] = '\0';
  307. }
  308.  
  309.  
  310.  
  311.  
  312. //-----------------------------------------------------------------------------
  313. // Name: LoadSegmentFile()
  314. // Desc: 
  315. //-----------------------------------------------------------------------------
  316. HRESULT LoadSegmentFile( HWND hDlg, TCHAR* strFileName )
  317. {
  318.     HRESULT hr;
  319.  
  320.     SetDlgItemText( hDlg, IDC_FILENAME, TEXT("") );
  321.  
  322.     // Free any previous segment, and make a new one
  323.     SAFE_DELETE( g_pMusicSegment );
  324.  
  325.     // Have the loader collect any garbage now that the old 
  326.     // script has been released
  327.     g_pMusicManager->CollectGarbage();
  328.  
  329.     // Set the media path based on the file name (something like C:\MEDIA)
  330.     // to be used as the search directory for finding DirectMusic content
  331.     // related to this file.
  332.     TCHAR strMediaPath[MAX_PATH];
  333.     _tcscpy( strMediaPath, strFileName );
  334.     TCHAR* strLastSlash = _tcsrchr(strMediaPath, TEXT('\\'));
  335.     *strLastSlash = 0;
  336.     if( FAILED( hr = g_pMusicManager->SetSearchDirectory( strMediaPath ) ) )
  337.         return DXTRACE_ERR( TEXT("SetSearchDirectory"), hr );
  338.  
  339.     // For DirectMusic must know if the file is a standard MIDI file or not
  340.     // in order to load the correct instruments.
  341.     BOOL bMidiFile = FALSE;
  342.     if( strstr( strFileName, ".mid" ) != NULL ||
  343.         strstr( strFileName, ".rmi" ) != NULL ) 
  344.     {
  345.         bMidiFile = TRUE;
  346.     }
  347.  
  348.     // Load the file into a DirectMusic segment 
  349.     if( FAILED( g_pMusicManager->CreateSegmentFromFile( &g_pMusicSegment, strFileName, 
  350.                                                         TRUE, bMidiFile ) ) )
  351.     {
  352.         // Not a critical failure, so just update the status
  353.         return S_FALSE; 
  354.     }
  355.  
  356.     // Update the UI controls to show the segment is loaded
  357.     SetDlgItemText( hDlg, IDC_FILENAME, strFileName );
  358.     EnablePlayUI( hDlg, TRUE );
  359.  
  360.     return S_OK;
  361. }
  362.  
  363.  
  364.  
  365.  
  366. //-----------------------------------------------------------------------------
  367. // Name: ProcessDirectMusicMessages()
  368. // Desc: Handle DirectMusic notification messages
  369. //-----------------------------------------------------------------------------
  370. HRESULT ProcessDirectMusicMessages( HWND hDlg )
  371. {
  372.     HRESULT hr;
  373.     IDirectMusicPerformance8* pPerf = NULL;
  374.     DMUS_NOTIFICATION_PMSG* pPMsg;
  375.         
  376.     if( NULL == g_pMusicManager )
  377.         return S_OK;
  378.  
  379.     pPerf = g_pMusicManager->GetPerformance();
  380.  
  381.     // Get waiting notification message from the performance
  382.     while( S_OK == pPerf->GetNotificationPMsg( &pPMsg ) )
  383.     {
  384.         switch( pPMsg->dwNotificationOption )
  385.         {
  386.         case DMUS_NOTIFICATION_SEGEND:
  387.             if( pPMsg->punkUser )
  388.             {
  389.                 IDirectMusicSegmentState8* pSegmentState   = NULL;
  390.                 IDirectMusicSegment*       pNotifySegment   = NULL;
  391.                 IDirectMusicSegment8*      pNotifySegment8  = NULL;
  392.                 IDirectMusicSegment8*      pPrimarySegment8 = NULL;
  393.  
  394.                 // The pPMsg->punkUser contains a IDirectMusicSegmentState8, 
  395.                 // which we can query for the segment that the SegmentState refers to.
  396.                 if( FAILED( hr = pPMsg->punkUser->QueryInterface( IID_IDirectMusicSegmentState8,
  397.                                                                   (VOID**) &pSegmentState ) ) )
  398.                     return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  399.  
  400.                 if( SUCCEEDED( hr = pSegmentState->GetSegment( &pNotifySegment ) ) )
  401.                 {
  402.                     if( FAILED( hr = pNotifySegment->QueryInterface( IID_IDirectMusicSegment8,
  403.                                                                      (VOID**) &pNotifySegment8 ) ) )
  404.                         return DXTRACE_ERR( TEXT("QueryInterface"), hr );
  405.  
  406.                     // Get the IDirectMusicSegment for the primary segment
  407.                     pPrimarySegment8 = g_pMusicSegment->GetSegment();
  408.  
  409.                     // Figure out which segment this is
  410.                     if( pNotifySegment8 == pPrimarySegment8 )
  411.                     {
  412.                         // Update the UI controls to show the sound as stopped
  413.                         EnablePlayUI( hDlg, TRUE );
  414.                     }
  415.                 }
  416.  
  417.                 // Cleanup
  418.                 SAFE_RELEASE( pSegmentState );
  419.                 SAFE_RELEASE( pNotifySegment );
  420.                 SAFE_RELEASE( pNotifySegment8 );
  421.             }
  422.             break;
  423.         }
  424.  
  425.         pPerf->FreePMsg( (DMUS_PMSG*)pPMsg ); 
  426.     }
  427.  
  428.     return S_OK;
  429. }
  430.  
  431.  
  432.  
  433. //-----------------------------------------------------------------------------
  434. // Name: OnPlaySegment()
  435. // Desc: 
  436. //-----------------------------------------------------------------------------
  437. HRESULT OnPlaySegment( HWND hDlg )
  438. {
  439.     HRESULT hr;
  440.  
  441.     HWND hLoopButton = GetDlgItem( hDlg, IDC_LOOP_CHECK );
  442.     BOOL bLooped = ( SendMessage( hLoopButton, BM_GETSTATE, 0, 0 ) == BST_CHECKED );
  443.  
  444.     if( bLooped )
  445.     {
  446.         // Set the segment to repeat many times
  447.         if( FAILED( hr = g_pMusicSegment->SetRepeats( DMUS_SEG_REPEAT_INFINITE ) ) )
  448.             return DXTRACE_ERR( TEXT("SetRepeats"), hr );
  449.     }
  450.     else
  451.     {
  452.         // Set the segment to not repeat
  453.         if( FAILED( hr = g_pMusicSegment->SetRepeats( 0 ) ) )
  454.             return DXTRACE_ERR( TEXT("SetRepeats"), hr );
  455.     }
  456.  
  457.     // Play the segment and wait. The DMUS_SEGF_BEAT indicates to play on the 
  458.     // next beat if there is a segment currently playing. 
  459.     if( FAILED( hr = g_pMusicSegment->Play( DMUS_SEGF_BEAT ) ) )
  460.         return DXTRACE_ERR( TEXT("Play"), hr );
  461.  
  462.     EnablePlayUI( hDlg, FALSE );
  463.  
  464.     return S_OK;
  465. }
  466.  
  467.  
  468.  
  469.  
  470. //-----------------------------------------------------------------------------
  471. // Name: EnablePlayUI( hDlg,)
  472. // Desc: Enables or disables the Play UI controls 
  473. //-----------------------------------------------------------------------------
  474. VOID EnablePlayUI( HWND hDlg, BOOL bEnable )
  475. {
  476.     if( bEnable )
  477.     {
  478.         EnableWindow(   GetDlgItem( hDlg, IDC_LOOP_CHECK ), TRUE );
  479.         EnableWindow(   GetDlgItem( hDlg, IDC_STOP ),       FALSE );
  480.  
  481.         EnableWindow(   GetDlgItem( hDlg, IDC_PLAY ),       TRUE );
  482.         SetFocus(       GetDlgItem( hDlg, IDC_PLAY ) );
  483.     }
  484.     else
  485.     {
  486.         EnableWindow(  GetDlgItem( hDlg, IDC_LOOP_CHECK ), FALSE );
  487.         EnableWindow(  GetDlgItem( hDlg, IDC_STOP ),       TRUE );
  488.         SetFocus(      GetDlgItem( hDlg, IDC_STOP ) );
  489.         EnableWindow(  GetDlgItem( hDlg, IDC_PLAY ),       FALSE );
  490.     }
  491. }
  492.  
  493.  
  494.  
  495.  
  496. //-----------------------------------------------------------------------------
  497. // Name: OnChangeTool()
  498. // Desc: 
  499. //-----------------------------------------------------------------------------
  500. HRESULT OnChangeTool( HWND hDlg )
  501. {
  502.     HRESULT hr;
  503.     IDirectMusicTool* pSelectedTool;
  504.     LONG lCurSelection;
  505.  
  506.     lCurSelection = (LONG)SendDlgItemMessage( hDlg, IDC_TOOL_COMBO, CB_GETCURSEL, 0, 0 );
  507.     pSelectedTool = (IDirectMusicTool*) SendDlgItemMessage( hDlg, IDC_TOOL_COMBO, 
  508.                                                             CB_GETITEMDATA, lCurSelection, 0 );
  509.     if( pSelectedTool != g_pCurrentTool )
  510.     {
  511.         // Remove the current tool from the graph
  512.         if( g_pCurrentTool != NULL )
  513.         {
  514.             if( FAILED( hr = g_pGraph->RemoveTool( g_pCurrentTool ) ) )
  515.                 return DXTRACE_ERR( TEXT("RemoveTool"), hr );
  516.         }
  517.  
  518.         // Add the tool to the graph on all PChannels 
  519.         // and at the beginning of the graph. 
  520.         if( pSelectedTool != NULL )
  521.         {
  522.             if( FAILED( hr = g_pGraph->InsertTool( pSelectedTool, NULL, 0, 0 ) ) )
  523.                 return DXTRACE_ERR( TEXT("InsertTool"), hr );
  524.         }
  525.  
  526.         g_pCurrentTool = pSelectedTool;
  527.     }
  528.  
  529.     return S_OK;
  530. }
  531.  
  532.  
  533.  
  534.  
  535.